package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.common.BaseResponse;
import com.seecen.common.ResultUtils;
import com.seecen.exception.ErrorCode;
import com.seecen.exception.ThrowUtils;
import com.seecen.mapper.RegionMapper;
import com.seecen.pojo.entity.Region;
import com.seecen.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Override
    public BaseResponse<List<Region>> getChildRegions(Long parentId) {
        try {
            ThrowUtils.throwIf(parentId == null, ErrorCode.PARAMS_ERROR, "父ID不能为空");

            LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Region::getParentid, parentId)
                    .eq(Region::getIsdeleted, 0)
                    .orderByAsc(Region::getRegionid);

            List<Region> regionList = baseMapper.selectList(queryWrapper);
            return ResultUtils.success(regionList); // 成功时类型自动匹配
        } catch (Exception e) {
            log.error("查询子地区失败，parentId: {}", parentId, e);
            // 强制转换为方法声明的泛型类型：BaseResponse<List<Region>>
            return (BaseResponse<List<Region>>) ResultUtils.error(ErrorCode.SYSTEM_ERROR, "查询子地区失败");
        }
    }

    @Override
    public BaseResponse<List<Region>> getProvinceList() {
        try {
            LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Region::getLevel, 1)
                    .eq(Region::getIsdeleted, 0)
                    .orderByAsc(Region::getRegionid);

            List<Region> provinceList = baseMapper.selectList(queryWrapper);
            return ResultUtils.success(provinceList); // 成功时类型自动匹配
        } catch (Exception e) {
            log.error("查询省级地区失败", e);
            // 强制转换为BaseResponse<List<Region>>
            return (BaseResponse<List<Region>>) ResultUtils.error(ErrorCode.SYSTEM_ERROR, "查询省级地区失败");
        }
    }

    @Override
    public BaseResponse<Integer> getFastestArrivalTime(Long regionId) {
        try {
            ThrowUtils.throwIf(regionId == null, ErrorCode.PARAMS_ERROR, "地区ID不能为空");

            LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Region::getRegionid, regionId)
                    .eq(Region::getIsdeleted, 0);

            Region region = baseMapper.selectOne(queryWrapper);
            ThrowUtils.throwIf(region == null, ErrorCode.NOT_FOUND_ERROR, "未找到该地区信息");

            Integer fastestTime = region.getFastestarrivaltime();
            return ResultUtils.success(fastestTime); // 成功时类型自动匹配
        } catch (Exception e) {
            log.error("查询地区[{}]最快上门时间失败", regionId, e);
            // 强制转换为BaseResponse<Integer>
            return (BaseResponse<Integer>) ResultUtils.error(ErrorCode.SYSTEM_ERROR, "查询上门时间失败");
        }
    }

    @Override
    public BaseResponse<Integer> getMinArrivalTimeByParentId(Long parentId) {
        try {
            ThrowUtils.throwIf(parentId == null, ErrorCode.PARAMS_ERROR, "父地区ID不能为空");

            LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Region::getParentid, parentId)
                    .eq(Region::getIsdeleted, 0)
                    .isNotNull(Region::getFastestarrivaltime);

            List<Region> childRegions = baseMapper.selectList(queryWrapper);
            ThrowUtils.throwIf(childRegions.isEmpty(), ErrorCode.NOT_FOUND_ERROR, "未找到下属地区的上门时间信息");

            int minTime = childRegions.stream()
                    .mapToInt(Region::getFastestarrivaltime)
                    .min()
                    .orElse(-1);

            return ResultUtils.success(minTime); // 成功时类型自动匹配
        } catch (Exception e) {
            log.error("查询父地区[{}]下属地区最快上门时间失败", parentId, e);
            // 强制转换为BaseResponse<Integer>
            return (BaseResponse<Integer>) ResultUtils.error(ErrorCode.SYSTEM_ERROR, "查询下属地区上门时间失败");
        }
    }
}